package com.shinvent.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shinvent.common.Logger;
import com.shinvent.dao.BaseDao;
import com.shinvent.dao.T_USERDao;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml");
	    PrintWriter out = response.getWriter();
	    T_USERDao userDao = null;
	    
	    try{
	    	String userName = request.getParameter("u");
	    	String passWord = request.getParameter("p");
	    	UUID uuid = UUID.randomUUID();
	    	String sid = uuid.toString().replace("-", "");
	    	
	    	userDao = new T_USERDao();
	    	userDao.query("USERNAME = '" + userName + "'");
	    	
	    	if(userDao.gotoNext()){
	    		if(userDao.getString("PASSWORD").equalsIgnoreCase(passWord)){
	    			
	    			userDao.update("SID", sid, userDao.getString("USER_ID"));
	    			
	    			out.println("<?xml version=\"1.0\"?>");
	    			out.println("<USER>");
//	    			out.println("<USERNAME>");
//	    			out.println(userDao.getProperty("USERNAME")!=null ? userDao.getString("USERNAME") : "");
//	    			out.println("</USERNAME>");
//	    			out.println("<PASSWORD>");
//	    			out.println(userDao.getProperty("PASSWORD")!=null ? userDao.getString("PASSWORD") : "");
//	    			out.println("</PASSWORD>");
	    			out.println("<SID>");
	    			out.println(sid);
	    			out.println("</SID>");
	    			out.println("</USER>");
	    		}
	    	}
	    	
	    	release(userDao);
	    
	    }catch(Exception e){
	    	e.printStackTrace();
	    	Logger.logError(e);
	    }finally{
	    	release(userDao);
	    }
	    
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private void release(BaseDao dao){
		try{
			dao.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

}
